﻿
#ifndef TOOLS_H
#define TOOLS_H

//{{{ file documentation -------------------------------------------------------

/*!
 * @file     tools.h
 * @brief    dicodor tools class declaration ...
 */

//}}} --------------------------------------------------------------------------
//{{{ includes / predeclarations -----------------------------------------------

// @(#) $Id: tools.h 50 2012-05-21 12:47:32Z nsouchon-dico@altern.org $

#include <QtCore/QDebug>
#include <QtCore/QElapsedTimer>

#include <QtGui/QWidget>
#include <QtGui/QDesktopWidget>

//}}} --------------------------------------------------------------------------
//{{{ macros -------------------------------------------------------------------

#define CHRONO_START    QElapsedTimer __chronoPPJ; __chronoPPJ.start ();
#define CHRONO_END      qDebug () << QString ("%1 exécuté en %2 ms").arg (Q_FUNC_INFO).arg (__chronoPPJ.elapsed ());

//}}} --------------------------------------------------------------------------
//{{{ structures ---------------------------------------------------------------
//}}} --------------------------------------------------------------------------
//{{{ class Tools --------------------------------------------------------------

class               Tools
{
private:
    Tools ();

public:
    static int      abs (const int a);
    static int      min (const int a, const int b);
    static int      max (const int a, const int b);
    static int      min (const int a, const int b, const int c);
    static int      max (const int a, const int b, const int c);
    static int      min (const int a, const int b, const int c, const int d);
    static int      max (const int a, const int b, const int c, const int d);

    // manipulation de bits

    static int      bit (const uchar * data, const int num);
    static int      bitLSB (const uchar * data, const int num);

    static void     setBit (uchar * data, const int num);
    static void     setBitLSB (uchar * data, const int num);

    static void     clearBit (uchar * data, const int num);
    static void     clearBitLSB (uchar * data, const int num);

    static void     memCpy (quint8 * dest, const quint8 * source, const int count);

    static bool     fileCopyByBlocks (const QString & srcName, const QString & dstName, const int bufferSize);
};

//}}} --------------------------------------------------------------------------
//{{{ inline / template functions ----------------------------------------------

inline Tools::Tools () {}

inline int Tools::abs (const int a) { return (a > 0) ? a : -a; }

inline int Tools::min (const int a, const int b) { return (a < b) ? a : b; }
inline int Tools::max (const int a, const int b) { return (a > b) ? a : b; }

inline int Tools::min (const int a, const int b, const int c) { return min (a, min (b, c)); }
inline int Tools::max (const int a, const int b, const int c) { return max (a, max (b, c)); }

inline int Tools::min (const int a, const int b, const int c, const int d) { return min (min (a, b), min (c, d)); }
inline int Tools::max (const int a, const int b, const int c, const int d) { return max (max (a, b), max (c, d)); }

inline int  Tools::bit (const uchar * data, const int num)    { return (data[num >> 3] >> (7 - (num & 7))) & 1; }
inline int  Tools::bitLSB (const uchar * data, const int num) { return (data[num >> 3] >>      (num & 7))  & 1; }

inline void Tools::setBit (uchar * data, const int num)       { data[num >> 3] |=  (1 << (7 - (num & 7))); }
inline void Tools::setBitLSB (uchar * data, const int num)    { data[num >> 3] |=  (1 <<      (num & 7));  }

inline void Tools::clearBit (uchar * data, const int num)     { data[num >> 3] &= ~(1 << (7 - (num & 7))); }
inline void Tools::clearBitLSB (uchar * data, const int num)  { data[num >> 3] &= ~(1 <<      (num & 7));  }

inline void Tools::memCpy (quint8 * dest, const quint8 * source, const int count)
{
    for (int index = 0 ; index != count / 4 ; index++) *(quint32 *) dest++ = *(quint32*) source++;
    for (int index = 0 ; index != count % 4 ; index++) *(quint8  *) dest++ = *(quint8 *) source++;
}

//}}} --------------------------------------------------------------------------

#endif // TOOLS_H

